package parser

import (
	"L.Go/src/crawler/engine"
	"L.Go/src/crawler/model"
	"regexp"
	"strconv"
)

var ageRe = regexp.MustCompile(`<td><span class="label">年龄：</span>([\d]+)岁</td>`)
var MarriageRe = regexp.MustCompile(`<td><span class="label">婚况：</span>[^<]+</td>`)

func ParseProFile(contents []byte, name string) engine.ParserResult {
	proFile := model.ProFile{}
	proFile.Name = name;
	age, err := strconv.Atoi(extractString(contents, ageRe))
	if err != nil {
		proFile.Age = age
	}
	proFile.Marriage = extractString(contents, MarriageRe)

	result := engine.ParserResult{
		Items: []interface{}{proFile},
	}
	return result
}

func extractString(contents []byte, re *regexp.Regexp) string {
	match := re.FindSubmatch(contents)
	if len(match) >= 2 {
		return string(match[1])
	} else {
		return ""
	}
}
